package com.ruoyi.project.dict.dictsentence.controller;

import com.ruoyi.common.utils.TableSplitUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.dictsentence.domain.CiSentence;
import com.ruoyi.project.dict.dictsentence.service.ICiSentenceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 造句管理Controller
 *
 * @author shixiangwen
 * @date 2020-09-22
 */
@Controller
@RequestMapping("/dict/dictsentence")
public class CiSentenceController extends BaseController {

    private String prefix = "dict/dictsentence";

    @Autowired
    private ICiSentenceService ciSentenceService;

    @RequiresPermissions("dict:dictsentence:view")
    @GetMapping()
    public String dictsentence() {
        return prefix + "/dictsentence";
    }

    /**
     * 查询造句管理列表
     */
    @RequiresPermissions("dict:dictsentence:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CiSentence ciSentence) {
        startPage();
        List<CiSentence> list = ciSentenceService.selectCiSentenceList(ciSentence);
        return getDataTable(list);
    }

    /**
     * 导出造句管理列表
     */
    @RequiresPermissions("dict:dictsentence:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CiSentence ciSentence) {
        List<CiSentence> list = ciSentenceService.selectCiSentenceList(ciSentence);
        ExcelUtil<CiSentence> util = new ExcelUtil<CiSentence>(CiSentence.class);
        return util.exportExcel(list, "dictsentence");
    }

    /**
     * 新增造句管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存造句管理
     */
    @RequiresPermissions("dict:dictsentence:add")
    @Log(title = "造句管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CiSentence ciSentence) {
        return toAjax(ciSentenceService.insertCiSentence(ciSentence));
    }

    /**
     * 修改造句管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, String ci, ModelMap mmap) {
        CiSentence query = new CiSentence();
        query.setId(id);
        query.setTable(TableSplitUtil.getCiSentenceTable(ci));
        CiSentence ciSentence = ciSentenceService.selectCiSentenceById(query);
        mmap.put("ciSentence", ciSentence);
        return prefix + "/edit";
    }

    /**
     * 修改保存造句管理
     */
    @RequiresPermissions("dict:dictsentence:edit")
    @Log(title = "造句管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CiSentence ciSentence) {
        return toAjax(ciSentenceService.updateCiSentence(ciSentence));
    }

    /**
     * 删除造句管理
     */
    @RequiresPermissions("dict:dictsentence:remove")
    @Log(title = "造句管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id, String ci) {
        CiSentence query = new CiSentence();
        query.setId(id);
        query.setTable(TableSplitUtil.getCiSentenceTable(ci));
        return toAjax(ciSentenceService.deleteCiSentenceById(query));
    }
}
